<cfif Not IsDefined("session.id")>
		<cflocation url = "/ecada/" addToken = "no">
</cfif>
	
<cfif IsDefined("session.experiment")>
	<cfset experiment = session.experiment>
<cfelse>
	<cflocation url="/ecada/">
</cfif>

<cfif IsDefined("form.submit")>
	<cftransaction>
		<!---Delete the old stuff--->
		<cfswitch expression="#origType#">
			<cfcase value="1"><!---likert--->
				<!---<cfquery datasource="exp">
				
				</cfquery>--->
			</cfcase>
			
			<cfcase value="2"><!---text--->
				<!---Don't need to do anything--->
			</cfcase>
			
			<cfcase value="3"><!---radio--->
			<!---Create Radio Group--->	
				<cfquery datasource="exp">
					DELETE FROM radio
					WHERE radioGroupID = #origInfoID#
				</cfquery>	
				<cfquery datasource="exp">
					DELETE FROM radiogroup
					WHERE id = #origInfoID#
				</cfquery>
			</cfcase>
			
			
			<cfcase value="4"><!---checkbox--->
				<cfquery datasource="exp">
					DELETE FROM Checkbox
					WHERE CheckboxGroupID = #origInfoID#
				</cfquery>
				<cfquery datasource="exp">
					DELETE FROM CheckboxGroup
					WHERE id = #origInfoID#
				</cfquery>
			</cfcase>
		</cfswitch>
		
		<cfquery datasource="exp" name="convert">
			SELECT *
			FROM qestiontype
			WHERE id = #origType#
		</cfquery>

	
		<cfswitch expression="#Atypes#">
		
			<cfcase value="likert">
				<!---incomplete--->
			</cfcase>
		
			<cfcase value="text">
				<cfif convert.type neq "text">
					<cfquery datasource="exp">
						UPDATE question
						SET question = '#Question#',
							 typeID = (SELECT * FROM qestiontype WHERE type='text'),
							 QuestionInfoID = -1
						WHERE id = #questionID#
					</cfquery>
				<cfelse>
					<cfquery datasource="exp">
						UPDATE question
						SET question = '#Question#'
						WHERE id = #questionID#
					</cfquery>
				</cfif>
			</cfcase>
		
			<cfcase value="radio">
				
				<!---Create Radio Group--->
				<cfquery datasource="exp" result="radGrp">
					INSERT INTO radiogroup(Title)
					VALUES('AAAAAAAAAA')
				</cfquery>
			
				<!---loop over all radio button entries ignoring any that have been deleted--->
				<cfset i = 1>
				
				<cfloop condition="i lte rbcount">

					<cfif IsDefined("form.rb" & i)>
				
						<cfquery datasource="exp">
							INSERT INTO radio(text, radioGroupID)
							VALUES('#form['rb' & i]#', #radGrp.GENERATED_KEY#)
						</cfquery>
						<cfset i += 1>
					</cfif>
				</cfloop>

				<cfif convert.type neq "radio">

					
					<cfquery datasource="exp">
						UPDATE question
						SET question = '#Question#',
							 QuestionInfoId = #radGrp.GENERATED_KEY#,
							 typeID = (SELECT qt.id FROM qestiontype qt WHERE qt.type = 'radio')
						WHERE id = #questionID#
					</cfquery>
					
				<cfelse>	

					<!---insert question into database--->
					<cfquery datasource="exp">
						UPDATE question
						SET question = '#Question#',
							 QuestionInfoId = #radGrp.GENERATED_KEY#
						WHERE id = #questionID#
					</cfquery>
				</cfif>
			</cfcase>
		
				<cfcase value="checkbox">
				<!---Create CheckboxGroup--->
				<cfquery datasource="exp" result="CBGrp">
					INSERT INTO CheckboxGroup(Title)
					VALUES('AAAAAAAAAA')
				</cfquery>
			
				<!---loop over all radio button entries ignoring any that have been deleted--->
				<cfset i = 1>
				<cfloop condition="i lte cbcount">
					
					<cfif IsDefined("form.cb" & i)>
						<cfquery datasource="exp">
							INSERT INTO Checkbox(Text, CheckboxGroupID)
							VALUES('#form['cb' & i]#', #CBGrp.GENERATED_KEY#)
						</cfquery>
						<cfset i += 1>
					</cfif>
				</cfloop>
						
				<cfif convert.type neq "checkbox">
					<cfquery datasource="exp">
							UPDATE question
							SET question = '#Question#',
								 QuestionInfoId = #CBGrp.GENERATED_KEY#,
							 	 typeID = (SELECT * FROM qestiontype WHERE type='checkbox')
							WHERE id = #questionID#
					</cfquery>
				<cfelse>	
					<cfquery datasource="exp">
							UPDATE question
							SET question = '#Question#',
								 QuestionInfoId = #CBGrp.GENERATED_KEY#
							WHERE id = #questionID#
					</cfquery>
				</cfif>
			</cfcase>
		</cfswitch>
	</cftransaction>
	<cflocation url="./../" addToken="no">
<cfelse>
	<cfquery datasource="exp" name="Question">
		SELECT *
		FROM question
		WHERE id = #EditID#
	</cfquery>
	<cfinclude template= "/ecada/header.cfm">
	<cfoutput>
		<form method="post" action="./">
					<fieldset>
			<legend>
				Question
			</legend>
			
			<table>
				<tr>
					<td>
						Question
					</td>
					<td>
						<input type="textbox" name="Question" value="#HTMLEditFormat(Question.question)#" id="Question">
					</td>
				</tr>
			<!---	<tr>
					<td>	
						Max Question Time
					</td>
					<td>
						<input type="textbox" value="0" disabled="true" name="MaxQ" id="MaxQ">(disabled)
						<br>
					</td>
				</tr>--->
			</table>
		</fieldset>
		<fieldset>
				<legend>
					Answer Type
				</legend>
				<select name="Atypes" id="Atypes">
					<cfquery datasource="exp" name="AnswerType">
						SELECT id,type
						FROM qestiontype
						ORDER BY type ASC
					</cfquery>
					<cfloop query="AnswerType">
						<cfif id eq Question.typeID>
							<option value="#HTMLEditFormat(type)#" selected="true">#HTMLEditFormat(type)#</option>
						<cfelse>
							<option value="#HTMLEditFormat(type)#">#HTMLEditFormat(type)#</option>
						</cfif>
					</cfloop>
				</select>
				<script type="text/javascript">
					$(document).ready(function() {
						var sel =  $('##Atypes').val();
						$('##Answer div').hide();
						$('.Answer-' + sel).show();
						$('##TrialAnswerType').val(sel);
						redrawShadows();
					});

					$('##Atypes').change(function(){
						var sel =  $('##Atypes').val();
						$('##Answer div').hide();
						$('.Answer-' + sel).show();
						$('##TrialAnswerType').val(sel);
						redrawShadows();
					});
				</script>
				<div name="Answer" id="Answer">
					<cfset InfoID = Question.QuestionInfoID>
					<cfinclude template="./../Answer/text.cfm">
					<cfinclude template="./../Answer/radio.cfm">
					<cfinclude template="./../Answer/likert.cfm">
					<cfinclude template="./../Answer/checkbox.cfm">
				</div>
			</fieldset>
			
			<input type="hidden" name="origType" value="#Question.typeID#">
			<input type="hidden" name="origInfoID" value="#Question.QuestionInfoID#">
			<input type="hidden" name="questionID" value="#Question.id#">
			<input type="submit" name="submit" value="Submit"></input>
		</form>
	</cfoutput>
	<cfinclude template= "/ecada/footer.cfm">
</cfif>



